package com.ithei.多线程第二天.送快递;

public class Demo2 {
    public static void main(String[] args) {
        new Thread(() -> {
            int count = 0;
            while (true) {
                synchronized (Demo2.class) {
                    if (KuaiDi.count == 0) {
                        System.out.println("==============快递已经全部到位了=============");
                        break;
                    } else {
                        if (KuaiDi.flag) {
                            try {
                                Demo2.class.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        } else {
                            count++;
                            System.out.println("此处投递第" + count + "件快递");
                            KuaiDi.flag = true;
                            Demo2.class.notifyAll();
                        }
                    }
                }
            }
        }).start();
        new Thread(() -> {
            int count = 0;
            while (true) {
                synchronized (Demo2.class) {
                    if (KuaiDi.count == 0) {
                        System.out.println("==================快递已经全部取出===================");
                        break;
                    } else {
                        if (!KuaiDi.flag) {
                            try {
                                Demo2.class.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        } else {
                            count++;
                            System.out.println("这是第" + count + "件快递已经取出");
                            KuaiDi.flag = false;
                            KuaiDi.count--;
                            Demo2.class.notifyAll();
                        }
                    }
                }
            }
        }).start();
    }
}
